//统一的页面数据接口处理
function commonApi(context: any, local: any, mapping: object = {}) {
    /**
     * context 获取到的api数据
     * local 定义的接口数据格式
     * mapping 需要映射的字段
     */

    const isArray = context instanceof Array;
    if (isArray) {
        return context.map(item => {
            return dataProcessing(local, item);
        });
    } else {
        return dataProcessing(local, context);
    }

    function dataProcessing(data, item) {
        if (JSON.stringify(mapping) !== "{}") {
            for (let i in mapping) {
                const getval = mapping[i];
                item[i] = item[getval];
                delete item[getval];
            }
            return Object.assign(data, item);
        } else {
            return Object.assign(data, item);
        }
    }
}

//左侧菜单数据过滤
function menuFilter(val: any, list: any[], fun: any, key: any) {
    const name = val.routes.name;
    console.log(name);
    if (list.length > 0) {
        for (let i = 0; i < list.length; i++) {
            const routes = list[i].routes;
            for (let j = 0; j < routes.length; j++) {
                let count = 0;
                const value = routes[j];
                if (value.name === name) {
                    fun(routes);
                    key(value.key,value);
                    break;
                }
                if (count > 0) {
                    break;
                }
                if (value.children && value.children.length > 0) {
                    for (let u = 0; u < value.children.length; u++) {
                        const children = value.children[u];
                        if (children.name === name) {
                            fun(routes);
                            key(value.key,children);
                            count++;
                            break;
                        }
                    }
                }
            }
        }
    }
}

export {commonApi, menuFilter};
